/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sms.web;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.utils.ExpertUtils;
import com.chlitina.store.modules.sms.entity.StoreDiagnose;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseDetail;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseTemplate;
import com.chlitina.store.modules.sms.entity.StoreDiagnoseTemplateDetail;
import com.chlitina.store.modules.sms.service.StoreDiagnoseService;
import com.chlitina.store.modules.sms.service.StoreDiagnoseSettingService;
import com.chlitina.store.modules.sms.service.StoreDiagnoseTemplateDetailService;
import com.chlitina.store.modules.sys.entity.Area;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;

/**
 * 店面诊断结果Controller
 * @author liujiang
 * @version 2013-08-01
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/sms/storeDiagnose")
public class StoreDiagnoseController extends BaseController {

	@Autowired
	private StoreDiagnoseService storeDiagnoseService;
	@Autowired
	private StoreDiagnoseSettingService storeDiagnoseSettingService;
	@Autowired
	private StoreDiagnoseTemplateDetailService storeDiagnoseTemplateDetailService;
	
	@ModelAttribute
	public StoreDiagnose get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeDiagnoseService.get(id);
		}else{
			return new StoreDiagnose();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(StoreDiagnose storeDiagnose, HttpServletRequest request, HttpServletResponse response, Model model) {
		Store store = StoreUtils.getStore();
		Page<StoreDiagnose> page = new Page<StoreDiagnose>(request, response);
		Page<StoreDiagnose> current_page = storeDiagnoseService.find(page, store.getId()); 
		model.addAttribute("page", current_page);
		return "modules/sms/storeDiagnoseList";
	}

	@RequestMapping(value = "form")
	public String form(StoreDiagnose storeDiagnose, Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		List<StoreDiagnoseTemplateDetail> list = Lists.newArrayList();
		StoreDiagnoseTemplate result = storeDiagnoseSettingService.getTemplateByArea(store.getArea());
		if(result == null) {
			model.addAttribute("message", "店面没有对应计算模板");
		} else {
			StoreDiagnose s = storeDiagnoseService.getLastByStore(store);
			if(s == null) {
				list = result.getDetailList();
			} else {
				Date lastDate = s.getDiagnose_date();
				Calendar cal = Calendar.getInstance();
				cal.setTime(lastDate);
				cal.add(Calendar.MONTH, StoreDiagnose.PER_MOUTHS);
				cal.add(Calendar.DAY_OF_MONTH, 1);
				if(DateUtils.getBeginOfDay(cal.getTime()).getTime() <= DateUtils.getBeginOfDay(new Date()).getTime()) {
					list = result.getDetailList();
				} else {
					model.addAttribute("message", "上次诊断时间:" + DateUtils.formatDate(lastDate)+", " +
							"间隔时间为" + StoreDiagnose.PER_MOUTHS +"个月, 下次开始诊断时间:" + DateUtils.formatDate(cal.getTime()));
				}
			}
		}
		Map<String, List<StoreDiagnoseTemplateDetail>> map = new HashMap<String, List<StoreDiagnoseTemplateDetail>>();
		for (StoreDiagnoseTemplateDetail s : list) {
			String key = s.getItem_group();
			List<StoreDiagnoseTemplateDetail> l = map.get(key);
			if(l == null) {
				l = Lists.newArrayList();
			} 
			if(!l.contains(s)){
				l.add(s);
			}
			map.put(key, l);
		} 
		model.addAttribute("templateDetailMap", map);
		return "modules/sms/storeDiagnoseDetailForm";
	}

	@RequestMapping(value = "save")
	public String save(StoreDiagnose storeDiagnose, Model model, RedirectAttributes redirectAttributes) {
		storeDiagnose.setStore(StoreUtils.getStore());
		storeDiagnose.setDiagnose_date(new Date());
		if (!beanValidator(model, storeDiagnose)){
			return form(storeDiagnose, model, redirectAttributes);
		}
		List<StoreDiagnoseDetail> list = storeDiagnose.getDetailList();
		for (StoreDiagnoseDetail s : list) {
			s.setStoreDiagnoseTemplateDetail(storeDiagnoseTemplateDetailService.get(s.getStoreDiagnoseTemplateDetail().getId()));
			s.setStoreDiagnose(storeDiagnose);
		}
		storeDiagnoseService.save(storeDiagnose);
		
		addMessage(redirectAttributes, "保存店面诊断结果成功");
		return "redirect:"+Global.ADMIN_PATH+"/sms/storeDiagnose";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		storeDiagnoseService.delete(id);
		addMessage(redirectAttributes, "删除店面诊断结果成功");
		return "redirect:"+Global.ADMIN_PATH+"/sms/storeDiagnose";
	}
}
